﻿using System;
using System.Linq;
using TestWebApplication.CommercePaymentInstrument;
using Microsoft.Commerce.Proxy.PaymentInstrumentService;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;

namespace TestWebApplication.CommerceTestDataProvider
{
    public class CommercePaymentInstrumentDataProvider
    {
        public static CallerInfo CreateCallerInfo(CommerceAccountProxy.Identity identity, string acccountId)
        {
            Identity requester = new Identity()
            {
                IdentityType = identity.IdentityType,
                IdentityValue = identity.IdentityValue,
            };
            requester.IdentityProperty = identity.IdentityProperty == null ? null : identity.IdentityProperty.Select(
                x => new Property() 
                {
                    Name = x.Name,
                    Value = x.Value,
                    Namespace = x.Namespace
                }).ToArray();

            CallerInfo ret = new CallerInfo()
            {
                Requester = requester,
                AccountId = acccountId,
            };
            return ret;
        }

        public static Identity CreateRandomIdentity()
        {
            // we only create "PUID" identity type here
            Puid puid = Puid.RandomPuid();
            Identity identity = new Identity();
            identity.IdentityType = "PUID";
            identity.IdentityValue = puid.ToString();

            identity.IdentityProperty = new[]
            {
                new Property
                {
                    Name = "PassportMemberName",
                    Value = GlobalRandom.CreateAlphabeticString(10) + "@live.com"
                }
            };

            return identity;
        }

        public static PaymentInstrument CreateUpdatedPaymentInstrument(PaymentInstrument originalPaymentInstrument)
        {
            //here we just change the address info and do update
            originalPaymentInstrument.AddressInfo = CreateRandomAddressInfo();

            return originalPaymentInstrument;
        }

        public static PaymentInstrument CreatePaymentInstrumentInfo(CommercePaymentInstrumentType piType, string encryptkey, bool? webMode)
        {
            PaymentInstrument pi = null;

            switch (piType)
            {
                case CommercePaymentInstrumentType.CREDITCARD:
                    string creditCardType = "VISA";
                    var accountNumber = CreditCardRandomizer.GetRandomAccountNumber(creditCardType);
                    pi = new CreditCardPaymentInstrument()
                    {
                        AccountHolderName = "Tony",
                        // encrypt the account number and cvm code
                        EncryptedAccountNumber = CommercePaymentInstrumentEncryptor.EncryptToByteArray(accountNumber, encryptkey),
                        EncryptedCVMCode = CommercePaymentInstrumentEncryptor.EncryptToByteArray(GlobalRandom.CreateNumericString(3), encryptkey),
                        AccountNumberLastFour = accountNumber.Substring((accountNumber.Length - 4), 4),
                        CardType = creditCardType,
                        ExpirationDate = DateTime.UtcNow.AddMonths(GlobalRandom.Next(12, 240)).ToString("MMyyyy")
                    };
                    break;

                case CommercePaymentInstrumentType.PAYPALPAYIN:
                    bool addOnWeb = webMode.HasValue ? webMode.Value : false;

                    pi = new PayPalPayinPaymentInstrument()
                    {
                        PayPalWebInfo = addOnWeb == false ? null : new PayPalWebInfo()
                        {
                            ReturnURL = "http://www.billing.com",
                            CancelURL = "http://www.billing.com",
                            Description = GlobalRandom.CreateAlphabeticString(4, 64),
                        },

                        PayPalOffWebInfo = addOnWeb == true ? null : new PayPalOffWebInfo()
                        {
                            AppDisplayName = "app display name",
                            AppName = "app name",
                            ClientPlatform = "Xbox",
                            DeviceCategory = "GameConsole",
                            DeviceId = Guid.NewGuid().ToString(),
                            DeviceIdType = "UDID",
                            DeviceModel = "device model",
                            DeviceName = "somebody's device",
                            SystemName = "windows phone 7",
                            SystemVersion = "7.0.0.0",
                        },
                        SourceType = addOnWeb ? PayPalSourceType.WEB : PayPalSourceType.OFFWEB,
                    };
                    break;

                default:
                    throw new ArgumentException("not support in sample code yet!");
            }

            pi.FriendlyName = GlobalRandom.CreateAlphabeticString(1, 64);
            pi.AddressInfo = CreateRandomAddressInfo();

            return pi;
        }

        private static Address CreateRandomAddressInfo()
        {
            return new Address
            {
                FriendlyName = GlobalRandom.CreateAlphabeticString(10, 21),
                Street1 = GlobalRandom.CreateAlphabeticString(10, 21),
                UnitNumber = GlobalRandom.Next(2000).ToString(),
                City = "NEW YORK",
                District = GlobalRandom.CreateAlphabeticString(10, 21),
                State = "NY",
                CountryCode = "US",
                PostalCode = "10001"
            };
        }
    }
}
